package com.feijia.aijingjingus;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.mob.MobSDK;

import java.util.HashMap;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;

import static com.feijia.aijingjingus.GlobalHelper.md5;


public class Smslogin extends AppCompatActivity {


    //private Button getyzm;
    private EventHandler eventHandler;
    private EditText etPassword;
    private EditText etConfirmPassword;
    private String Password,ConfirmPassword;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smslogin);

        etPassword = (EditText) findViewById(R.id.etPassword);
        etConfirmPassword = (EditText) findViewById(R.id.etConfirmPassword);


        /*getyzm = (Button)findViewById(R.id.get_yzm);
        getyzm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getSms(view);
            }
        });*/

    //初始化SDK
        MobSDK.init(this,"26e60d71d9250","fa62a681a9d3fb8d609a91f60eb554f5");

    // 创建EventHandler对象
    eventHandler =new EventHandler() {
        public void afterEvent ( int event, int result, Object data){
            if (data instanceof Throwable) {
                Throwable throwable = (Throwable) data;
                final String msg = throwable.getMessage();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(Smslogin.this, msg, Toast.LENGTH_SHORT).show();
                    }
                });
            } else {
                if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                    // 这里是验证成功的回调，可以处理验证成功后您自己的逻辑，需要注意的是这里不是主线程
                    Intent intent = new Intent();
                    intent.setClass(getApplicationContext(),Smslogin1.class);
                    startActivity(intent);
                }
            }
        }
    };

    // 注册监听器
        SMSSDK.registerEventHandler(eventHandler);
        /*btnSendMsg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                onClickEvent(v);
            }
        });*/


    // 打开注册页面
    RegisterPage registerPage = new RegisterPage();

    // 自定义模板编号，若不需使用自定义模板，可以不设置
    //registerPage.setTempCode("1397121");

        registerPage.setRegisterCallback(new EventHandler() {
        public void afterEvent ( int event, int result, Object data){
            // 解析注册结果
            if (result == SMSSDK.RESULT_COMPLETE) {
                @SuppressWarnings("unchecked")
                HashMap<String, Object> phoneMap = (HashMap<String, Object>) data;
                String country = (String) phoneMap.get("country");
                String phone = (String) phoneMap.get("phone");
                // 提交用户信息
                //registerUser(country, phone);
            }
        }
    });
        registerPage.show(this);

    }

    protected void onDestroy(){
        super.onDestroy();
        SMSSDK.unregisterEventHandler(eventHandler);
    }

    public void getpassword(View view) {
        //跳到检测页面
        //SMSSDK.getVerificationCode("86","15038707891");

        Password = etPassword.getText().toString().trim();
        Password = md5(Password);
        ConfirmPassword = etConfirmPassword.getText().toString().trim();
        ConfirmPassword = md5(ConfirmPassword);
        if(Password.isEmpty()||ConfirmPassword.isEmpty()){
            Toast.makeText(getApplicationContext(), getString(R.string.PasswordNoEmpty),
                    Toast.LENGTH_SHORT).show();
            return;
        }
        else if (Password.compareTo(ConfirmPassword)!=0) {
            Toast.makeText(getApplicationContext(), getString(R.string.PasswordMustSame),
                    Toast.LENGTH_SHORT).show();
            return;
        }
        else {
            Intent intent = new Intent();
            intent.setClass(this,controlCenterActivity.class);
            startActivity(intent);
        }

    }

    /*public void tijiao(View view){
        String number = usernumber.getText().toString();
        SMSSDK.submitVerificationCode("86","188****8888",number);
    }*/

}
